package cn.cyx.demo.chat.model;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 聊天房间数据模型
 * 
 * @author cyx
 * @since 1.0.0
 */
public class ChatRoom {
    
    /** 房间ID */
    private String roomId;
    
    /** 房间名称 */
    private String roomName;
    
    /** 房间描述 */
    private String description;
    
    /** 房间创建者 */
    private String creator;
    
    /** 房间创建时间 */
    private LocalDateTime createTime;
    
    /** 房间最大人数 */
    private int maxUsers;
    
    /** 房间当前人数 */
    private int currentUsers;
    
    /** 房间状态 */
    private RoomStatus status;
    
    /** 房间用户列表 */
    private Map<String, UserSession> users;
    
    /**
     * 房间状态枚举
     */
    public enum RoomStatus {
        /** 正常 */
        NORMAL,
        /** 锁定 */
        LOCKED,
        /** 已删除 */
        DELETED
    }
    
    public ChatRoom() {
        this.createTime = LocalDateTime.now();
        this.status = RoomStatus.NORMAL;
        this.users = new ConcurrentHashMap<>();
        this.maxUsers = 100;
    }
    
    public ChatRoom(String roomId, String roomName, String creator) {
        this();
        this.roomId = roomId;
        this.roomName = roomName;
        this.creator = creator;
    }
    
    /**
     * 添加用户到房间
     * 
     * @param userSession 用户会话
     * @return 是否添加成功
     */
    public boolean addUser(UserSession userSession) {
        if (currentUsers >= maxUsers) {
            return false;
        }
        
        users.put(userSession.getUserId(), userSession);
        userSession.setRoomId(roomId);
        currentUsers = users.size();
        return true;
    }
    
    /**
     * 从房间移除用户
     * 
     * @param userId 用户ID
     * @return 被移除的用户会话
     */
    public UserSession removeUser(String userId) {
        UserSession userSession = users.remove(userId);
        if (userSession != null) {
            userSession.setRoomId(null);
            currentUsers = users.size();
        }
        return userSession;
    }
    
    /**
     * 获取房间用户列表
     * 
     * @return 用户会话映射
     */
    public Map<String, UserSession> getUsers() {
        return users;
    }
    
    /**
     * 检查房间是否已满
     * 
     * @return 是否已满
     */
    public boolean isFull() {
        return currentUsers >= maxUsers;
    }
    
    /**
     * 检查房间是否为空
     * 
     * @return 是否为空
     */
    public boolean isEmpty() {
        return currentUsers == 0;
    }
    
    // Getters and Setters
    public String getRoomId() {
        return roomId;
    }
    
    public void setRoomId(String roomId) {
        this.roomId = roomId;
    }
    
    public String getRoomName() {
        return roomName;
    }
    
    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getCreator() {
        return creator;
    }
    
    public void setCreator(String creator) {
        this.creator = creator;
    }
    
    public LocalDateTime getCreateTime() {
        return createTime;
    }
    
    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }
    
    public int getMaxUsers() {
        return maxUsers;
    }
    
    public void setMaxUsers(int maxUsers) {
        this.maxUsers = maxUsers;
    }
    
    public int getCurrentUsers() {
        return currentUsers;
    }
    
    public void setCurrentUsers(int currentUsers) {
        this.currentUsers = currentUsers;
    }
    
    public RoomStatus getStatus() {
        return status;
    }
    
    public void setStatus(RoomStatus status) {
        this.status = status;
    }
    
    @Override
    public String toString() {
        return "ChatRoom{" +
                "roomId='" + roomId + '\'' +
                ", roomName='" + roomName + '\'' +
                ", description='" + description + '\'' +
                ", creator='" + creator + '\'' +
                ", createTime=" + createTime +
                ", maxUsers=" + maxUsers +
                ", currentUsers=" + currentUsers +
                ", status=" + status +
                '}';
    }
} 